﻿using ProductSystem.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProductSystem.BLL.Common
{
    public class DataAddGeneric<T> where T : class
    {
        public delegate bool IsExist(T item);

        /// <summary>
        /// 判断数据库是否存在该记录的委托
        /// </summary>
        public static IsExist isExist { get; set; }

        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="item">要添加到数据库的记录</param>
        /// <returns></returns>
        public static ReturnStatus Add(T item)
        {
            ReturnStatus rs = new ReturnStatus();
            try
            {
                using (EntitiesModel dbContext = new EntitiesModel())
                {
                    if (isExist != null)
                    {
                        if (isExist(item)) return PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptAddExist);
                    }
                    dbContext.Add(item);
                    dbContext.SaveChanges();
                }
                rs = PromptMessage.Instance().GetPrompt(true, TBConfig.Instance().GetPromptAddSuccess);
            }
            catch
            {
                rs = PromptMessage.Instance().GetPrompt(false, TBConfig.Instance().GetPromptAddFail);
            }
            return rs;
        }
    }
}
